<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="6">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" disabled placeholder="请输入单据编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="入库单" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="inspectionCode">
              <a-input-search v-model="model.inspectionCode"
                              placeholder="请选择入库单"
                              @search="onSearchProcurement"></a-input-search>
              <!--                  <a-input v-model="row.procurementCode" disabled />-->
            </a-form-model-item>
<!--            <a-form-model-item label="质检单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspectionId">-->
<!--              <a-input v-model="model.inspectionId" placeholder="请输入质检单"  ></a-input>-->
<!--            </a-form-model-item>-->
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="质检员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspector">
              <a-input v-model="model.inspector" disabled placeholder="请输入返工地点"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="检查时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspectionDate">
              <a-input v-model="model.inspectionDate" disabled placeholder="请输入检查时间"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-tab-pane tab="检验单关联入库明细" :key="refKeys[0]" :forceRender="true">
            <j-vxe-table
              keep-source
              :ref="refKeys[0]"
              :loading="bussInspectionFormStorageTable.loading"
              :columns="bussInspectionFormStorageTable.columns"
              :dataSource="bussInspectionFormStorageTable.dataSource"
              :maxHeight="300"
              :disabled="formDisabled"
              :rowNumber="true"
              :rowSelection="true"
              :toolbar="false"
            />
          </a-tab-pane>
        </a-row>
        <a-row>
          <a-tab-pane tab="质检明细" :key="refKeys[1]" :forceRender="true">
            <j-vxe-table
              keep-source
              :ref="refKeys[1]"
              :loading="bussInspectionFormDetailTable.loading"
              :columns="bussInspectionFormDetailTable.columns"
              :dataSource="bussInspectionFormDetailTable.dataSource"
              :maxHeight="300"
              :disabled="formDisabled"
              :rowNumber="true"
              :rowSelection="false"
              :toolbar="false"
            />
          </a-tab-pane>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="处理意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="handlingOpinions">
              <j-dict-select-tag type="radio" v-model="model.handlingOpinions" @change="onChangeShowRow" dictCode="cl_type" placeholder="请选择处理意见" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="处理人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processedBy">
              <j-search-select-tag v-model="model.processedBy" dict="sys_user,realname,username" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="处理时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processedTime">
              <j-date placeholder="请选择处理时间" v-model="model.processedTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="this.model.type === '1'">
          <a-col :span="6">
            <a-form-model-item label="返工数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reworkNum">
              <a-input-number v-model="model.reworkNum" placeholder="请输入返工数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="返工地点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reworkLocation">
              <a-input v-model="model.reworkLocation" placeholder="请输入返工地点"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="返工方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reworkType">
              <a-input v-model="model.reworkType" placeholder="请输入返工方式"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="this.model.type === '2'">
          <a-col :span="12">
            <a-form-model-item label="扣款内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deductionContent">
              <a-input v-model="model.deductionContent" placeholder="请输入扣款内容"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="扣款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deductionAmount">
              <a-input-number v-model="model.deductionAmount" placeholder="请输入扣款金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
    <buss-inspection-list-modal ref="selectInspectionList"
                             @inspectionOk="inspectionOk"
                             :category="category">

    </buss-inspection-list-modal>
  </a-spin>
</template>

<script>

import { httpAction, getAction, putAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import BussInspectionListModal  from '@comp/base/modal/BussInspectionListModal'
  export default {
    name: 'BussUnqualifiedFormForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
      BussInspectionListModal
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          type:0,
         },
        category: '2',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        refKeys: ['bussInspectionFormStorage', 'bussInspectionFormDetail', ],
        tableKeys:['bussInspectionFormStorage', 'bussInspectionFormDetail', ],
        bussInspectionFormStorageTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '入库单编号',
              key: 'storageCode',
              width:"150px",
            },
            {
              title: '物料编号',
              key: 'materialCode',
              width: '120px',
            },
            {
              title: '物料名称',
              key: 'materialName',
              width: '150px',

            },
            {
              title: '单位',
              key: 'meUnit',
              type: JVXETypes.select,
              width: '80px',
              dictCode: 'ji_unit',
              disabled: true
            },
            {
              title: '规格',
              key: 'spec',
              width: '100px',
            },
            {
              title: '入库数量',
              key: 'num',
              width:"80px",
            },
            {
              title: '排产单号',
              key: 'schedulingNumber',
              width:"120px",
            },
            {
              title: '批量(吨)',
              key: 'batch',
              width:"80px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '物料来源',
              key: 'materialSource',
              width:"120px",
            },
            {
              title: '客户名称',
              key: 'customerName',
              width:"120px",
            },
            {
              title: '限用日期',
              key: 'expiryDate',
              width:"120px",
            },
            {
              title: '生产日期',
              key: 'createDate',
              width:"120px",
            },
          ]
        },
        // 质检明细
        bussInspectionFormDetailTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '检验项目',
              key: 'inspectionItems',
              width:"200px",
            },
            {
              title: '检验标准',
              key: 'inspectionStandards',
              width:"200px",
            },
            {
              title: '标准类型',
              key: 'standardType',
              type: JVXETypes.select,
              dictCode:"standard_type",
              width:"120px",
              disabled: true
            },
            {
              title: '数值上限',
              key: 'upperLimit',
              width:"120px",
            },
            {
              title: '数值下限',
              key: 'lowerLimit',
              width:"120px",
            },
            {
              title: '检验数值',
              key: 'inspectionNumber',
              width:"120px",
            },
            {
              title: '复检',
              key: 'reInspection',
              width:"120px",
            },
            {
              title: '检验结果',
              key: 'inspectionResults',
              type: JVXETypes.select,
              dictCode:"jy_results",
              width:"120px",
              disabled: true
            },
          ]
        },
        url: {
          add: "/quality/bussUnqualifiedForm/add",
          edit: "/quality/bussUnqualifiedForm/edit",
          queryById: "/quality/bussUnqualifiedForm/queryById",
          bussInspectionFormStorage: {
            list: '/quality/bussInspectionForm/queryBussInspectionFormStorageByMainId',
          },
          bussInspectionFormDetail: {
            list: '/quality/bussInspectionForm/queryBussInspectionFormBhgDetailByMainId',
          },
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      getSequenceNum() {
        let url = "/sys/fillRule/executeRuleByCode/unqualified_code";
        putAction(url, {}).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            this.model.code = res.result;
          }
        })
      },
      onChangeShowRow() {
        console.log(this.model.handlingOpinions)
        this.model.type = this.model.handlingOpinions;
      },
      onSearchProcurement(){
        // 采购订单
        this.$nextTick(() => {
          this.$refs.selectInspectionList.add();
        })
      },
      inspectionOk(res) {
        console.log("123")
        this.model.inspectionId = res.id;
        this.model.inspectionCode = res.code;
        this.model.inspector = res.inspector;
        this.model.inspectionDate = res.inspectionDate;
        let params = { id: res.id }
        this.requestSubTableData(this.url.bussInspectionFormStorage.list, params, this.bussInspectionFormStorageTable)
        this.requestSubTableData(this.url.bussInspectionFormDetail.list, params, this.bussInspectionFormDetailTable)
      },
      add () {
        this.edit(this.modelDefault);
      },
      addBefore(){
        this.bussInspectionFormStorageTable.dataSource=[]
        this.bussInspectionFormDetailTable.dataSource=[]
      },
      edit (record) {
        if (!record.id){
          this.getSequenceNum()
          record.code=this.model.code
        }

        this.model = Object.assign({}, record);
        this.visible = true;
        this.editAfter();
      },
      editAfter() {
        console.log(this.model);
        // 加载子表数据
        if (this.model.inspectionId) {
          let params = { id: this.model.inspectionId }
          this.requestSubTableData(this.url.bussInspectionFormStorage.list, params, this.bussInspectionFormStorageTable)
          this.requestSubTableData(this.url.bussInspectionFormDetail.list, params, this.bussInspectionFormDetailTable)
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>